/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2023-03-01
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "bsp.h"
#include "pincfg.h"
#include "bsp_delay.h"
#include "apm32f4xx_misc.h"
#include "apm32f4xx_usart.h"
#include "usb_host_user.h"
#include "..\Fat_Fs\fatfs.h"
#include "framework.h"
#include <stdio.h>
#include <io_flt_sig.h>

/*****************************************************************************
 * @brief   var.
 *****************************************************************************/
FATFS fatfs;
FIL file;
FIL fileR;
DIR dir;
FILINFO fno;

io_flt_edge_type usb_key1;
io_flt_edge_type usb_key2;

/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/
void usb_task(const void *argument);
void spdf_task(char en);

/*****************************************************************************
 * @brief   type.
 *****************************************************************************/
typedef enum
{
	USER_APP_INIT,
	USER_APP_RUN,
} USER_APP_STATE_T;

/******************************************************************************
 * @brief   usb task creat.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
osThreadDef(thd_usb, usb_task, osPriorityLow, 1, 0x800);
obj_init_order5(usb_task_creat);
void usb_task_creat(void)
{
	os_obj.tid_usb_task = osThreadCreate(osThread(thd_usb), NULL);
}

/******************************************************************************
 * @brief   usb task
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void usb_task(const void *argument)
{
	/* Init USB host */
	USB_HostInit();

	dbg_print("USB Host MSC HS2 Application\n");
	dbg_print("Please insert the U disk for read and write file\n");

	while (1)
	{
		USB_HostProcess();
		USB_HostUserApplication();

		io_flt_edge(&usb_key1, 1, 8, key1.in_st() ? 0 : 1);
		io_flt_edge(&usb_key2, 1, 8, key2.in_st() ? 0 : 1);
		osDelay(1);
	}
}

/*!
 * @brief       User application
 *
 * @param       None
 *
 * @retval      None
 */
void USB_HostUserApplication(void)
{
	uint8_t status = gUsbHostAppStatus;
	static uint8_t userAppState = USER_APP_INIT;

	switch (status)
	{
	case USBH_APP_CONNECTION:
		break;

	case USBH_APP_DISCONNECTION:
		userAppState = USER_APP_INIT;
		break;

	case USBH_APP_READY:
		switch (userAppState)
		{
		case USER_APP_INIT:
			dbg_print("\n***********OP Menu************\n");
			dbg_print("1 - Press KEY 1 to write file on the USB drive\n");
			dbg_print("2 - Press KEY 2 to read file from the USB drive\n");

			userAppState = USER_APP_RUN;
			break;

		case USER_APP_RUN:
			if (usb_key1.edge_found)
			{

				if (gUsbHostHS.devInfo.connectedStatus == ENABLE)
				{
					dbg_print("------ Write files to U disk ------ \n");

					if (f_mount(&fatfs, "0:", 0) == FR_OK)
					{
						FATFS_WriteFile(&file);
					}
					else
					{
						dbg_print("fatFs mount fail!\n");
					}

					spdf_task(1);
				}
			}

			if (usb_key2.edge_found)
			{

				if (gUsbHostHS.devInfo.connectedStatus == ENABLE)
				{
					dbg_print("------ read U disk file ------\n");

					if (f_mount(&fatfs, "0:", 0) == FR_OK)
					{
						FATFS_ReadFile(&file);
					}
					else
					{
						dbg_print("fatFs mount fail!\n");
					}
				}
			}
			break;
		}
		break;

	default:
		break;
	}
}

/**@} end of group OTGH_MSC_HS2_Functions */
/**@} end of group OTGH_MSC_HS2 */
/**@} end of group Examples */
